package dk.ucn.cm.builder;


import java.util.Random;

import com.touchmenotapps.widget.radialmenu.semicircularmenu.SemiCircularRadialMenu;
import com.touchmenotapps.widget.radialmenu.semicircularmenu.SemiCircularRadialMenuItem;
import com.touchmenotapps.widget.radialmenu.semicircularmenu.SemiCircularRadialMenuItem.OnSemiCircularRadialMenuPressed;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

	import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.graphics.drawable.Drawable;	
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
	

public class GameBoard extends Activity implements View.OnClickListener//, View.OnDragListener
{
	private SemiCircularRadialMenu mMenu, mMenu2, subMenu1;	
	private SemiCircularRadialMenuItem mArkitektur, mStedet, mBrugere, mStabilitet, mOekonomi, mEnergidesign, mTekniskeInstallationer, mIndustrialisering;
	private SemiCircularRadialMenuItem s1, s2, s3, s4;
	private Button getWords, green1, green2, orange1, orange2, red1, red2;
	private String[] greenWords, orangeWords, redWords;
	private static final Random random = new Random();
	
	//TODO: MAKE WORDBUTTONS DRAG&DROP
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_semi_circular_radial_menu);
		
		//TODO: implement drag and drop
		green1 = (Button)findViewById(R.id.btn_green1);
		green1.setOnLongClickListener(new MyClickListener()); //sets the listener to the dragging data
		findViewById(R.id.radial_menu).setOnDragListener(new MyDragListener());
		findViewById(R.id.radial_menu2).setOnDragListener(new MyDragListener());
		
		showMeTheButtons();
		getWords.setOnClickListener(this);	
		//green1.setOnDragListener(this);
		
		mArkitektur = new SemiCircularRadialMenuItem("Arkitektur", getResources().getDrawable(R.drawable.rotate_arkitektur), "Arkitektur");
		mStedet = new SemiCircularRadialMenuItem("Stedet", getResources().getDrawable(R.drawable.rotate_stedet), "Stedet");
		mBrugere = new SemiCircularRadialMenuItem("Brugere", getResources().getDrawable(R.drawable.rotate_brugere), "Brugere");
		mStabilitet = new SemiCircularRadialMenuItem("Stabilitet", getResources().getDrawable(R.drawable.rotate_stabilitet), "Stabilitet");
		mOekonomi = new SemiCircularRadialMenuItem("Oekonomi", getResources().getDrawable(R.drawable.rotate_oekonomi), "Oekonomi");
		mEnergidesign = new SemiCircularRadialMenuItem("Energidesign", getResources().getDrawable(R.drawable.rotate_energidesign), "Energidesign");
		mTekniskeInstallationer = new SemiCircularRadialMenuItem("TekniskeInstallationer", getResources().getDrawable(R.drawable.rotate_tekniske_installationer), "TekniskeInstallationer");
		mIndustrialisering = new SemiCircularRadialMenuItem("Industrialisering", getResources().getDrawable(R.drawable.rotate_industrialisering), "mIndustrialisering" );
		
		//TODO: set the menuitems to be shown always (no click needed to show menu)
		//TODO: make the menuitems non-clickable
		mMenu = (SemiCircularRadialMenu) findViewById(R.id.radial_menu);		
		mMenu.addMenuItem(mArkitektur.getMenuID(), mArkitektur);		
		mMenu.addMenuItem(mStedet.getMenuID(), mStedet);
		mMenu.addMenuItem(mBrugere.getMenuID(), mBrugere);
		mMenu.addMenuItem(mStabilitet.getMenuID(), mStabilitet);	
		mMenu2 = (SemiCircularRadialMenu) findViewById(R.id.radial_menu2);
		mMenu2.addMenuItem(mEnergidesign.getMenuID(), mEnergidesign);
		mMenu2.addMenuItem(mTekniskeInstallationer.getMenuID(), mTekniskeInstallationer);
		mMenu2.addMenuItem(mIndustrialisering.getMenuID(), mIndustrialisering);
		mMenu2.addMenuItem(mOekonomi.getMenuID(), mOekonomi);		
						
		//sets the background colors of each menu item
		mArkitektur.setBackgroundColor(getResources().getColor(R.color.Arkitektur));
		mStedet.setBackgroundColor(getResources().getColor(R.color.Stedet));
		mBrugere.setBackgroundColor(getResources().getColor(R.color.Brugere));
		mStabilitet.setBackgroundColor(getResources().getColor(R.color.Stabilitet));
		mEnergidesign.setBackgroundColor(getResources().getColor(R.color.Energidesign));
		mTekniskeInstallationer.setBackgroundColor(getResources().getColor(R.color.Tekniske_installationer));
		mOekonomi.setBackgroundColor(getResources().getColor(R.color.Oekonomi));
		mIndustrialisering.setBackgroundColor(getResources().getColor(R.color.Industrialisering));
		
		/*
		//SUBMENU
		s1 = new SemiCircularRadialMenuItem("s1", getResources().getDrawable(R.drawable.ic_launcher), "s1" );
		s2 = new SemiCircularRadialMenuItem("s2", getResources().getDrawable(R.drawable.ic_launcher), "s2" );
		s3 = new SemiCircularRadialMenuItem("s3", getResources().getDrawable(R.drawable.ic_launcher), "s3" );
		s4 = new SemiCircularRadialMenuItem("s4", getResources().getDrawable(R.drawable.ic_launcher), "s4" );
		subMenu1 = (SemiCircularRadialMenu) findViewById(R.id.radial_submenu1);
		subMenu1.addMenuItem(s1.getMenuID(), s1);
		subMenu1.addMenuItem(s2.getMenuID(), s2);
		subMenu1.addMenuItem(s3.getMenuID(), s3);
		subMenu1.addMenuItem(s4.getMenuID(), s4);	*/	
		
		/*mArkitektur.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mArkitektur.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mStedet.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mStedet.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mBrugere.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mBrugere.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mStabilitet.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mStabilitet.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mOekonomi.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mOekonomi.getText(), Toast.LENGTH_LONG).show();				
			}
		});
		
		mEnergidesign.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mEnergidesign.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mTekniskeInstallationer.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mTekniskeInstallationer.getText(), Toast.LENGTH_LONG).show();
			}
		});
		
		mIndustrialisering.setOnSemiCircularRadialMenuPressed(new OnSemiCircularRadialMenuPressed() 
		{
			@Override
			public void onMenuItemPressed() 
			{
				//Toast.makeText(GameBoard.this, mIndustrialisering.getText(), Toast.LENGTH_LONG).show();
			}
		});*/
	}
		
	private void showMeTheButtons() 
	{
		// TODO Auto-generated method stub
		getWords = (Button)findViewById(R.id.btn_getWords);
	}	

	@Override
	public void onClick(View view) 
	{
		// TODO Auto-generated method stub
		getMyWords();
	}
	
	//TODO: make the random words autosize to fit button
	public void getMyWords()
	{
		//GETTING RANDOM WORDS
				Resources res = getResources();
				greenWords = res.getStringArray(R.array.greenArray);
				String g1 = greenWords[random.nextInt(greenWords.length)];
				Button green1 = (Button)findViewById(R.id.btn_green1);
				green1.setText(g1);
				
				greenWords = res.getStringArray(R.array.greenArray);
				String g2 = greenWords[random.nextInt(greenWords.length)];
				Button green2 = (Button)findViewById(R.id.btn_green2);
				green2.setText(g2);
				
				orangeWords = res.getStringArray(R.array.orangeArray);
				String o1 = orangeWords[random.nextInt(orangeWords.length)];
				Button orange1 = (Button)findViewById(R.id.btn_yellow1);
				orange1.setText(o1);
				
				orangeWords = res.getStringArray(R.array.orangeArray);
				String o2 = orangeWords[random.nextInt(orangeWords.length)];
				Button orange2 = (Button)findViewById(R.id.btn_yellow2);
				orange2.setText(o2);
				
				redWords = res.getStringArray(R.array.redArray);
				String r1 = redWords[random.nextInt(redWords.length)];
				Button red1 = (Button)findViewById(R.id.btn_red1);
				red1.setText(r1);
				
				redWords = res.getStringArray(R.array.redArray);
				String r2 = redWords[random.nextInt(redWords.length)];
				Button red2 = (Button)findViewById(R.id.btn_red2);
				red2.setText(r2);
	}

	
	//DRAG & DROP FUNCTIONS	
	
	private final class MyClickListener implements OnLongClickListener
	{
		//called when the item is long-clicked
		@Override
		public boolean onLongClick(View view) 
		{
			//create it from the object's tag
			ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
			
			String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
			ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
			DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
			
			//data = data to be dragged, shadowBuilder = drag shadow, view = local data about the drag&drop operation
			view.startDrag(data, shadowBuilder, view, 0);
			
			view.setVisibility(View.INVISIBLE);
			return true;
		}		
	}
	
	class MyDragListener implements OnDragListener
	{
		Button green1 = (Button)findViewById(R.id.btn_green1);
		//Drawable targetShape = getResources().getDrawable(R.drawable.ic_launcher);
		//Drawable normalShape = getResources().getDrawable(R.drawable.brugere);

		@Override
		public boolean onDrag(View v, DragEvent event) 
		{
			//Handles each of the expected events
			switch(event.getAction())
			{
			//signal for the start of a drag and drop operation
			case DragEvent.ACTION_DRAG_STARTED:
				//do nothing
				break;
			
			//the drag point has entered the bounding box of the View
			//case DragEvent.ACTION_DRAG_ENTERED:
			//	v.setBackground(targetShape); //change the shape of the view
			//	break;
				
			//the user has moved the drag shadow outside the bounding box of the View
			//case DragEvent.ACTION_DRAG_EXITED:
			//	v.setBackground(normalShape); //change the shape of the view back to normal
			//	break;
				
			//drag shadow has been released, the drag point is within the bounding box of the View
			case DragEvent.ACTION_DROP:
				//if the view is the relativeLayout, we accept the drag item
				if(v == findViewById(R.id.RelativeLayout1))
				{
					View view = (View) event.getLocalState();
					ViewGroup viewgroup = (ViewGroup) view.getParent();
					viewgroup.removeView(view);
					
					//LinearLayout containView = (LinearLayout) v;
					RelativeLayout containView = (RelativeLayout) v;
					containView.addView(view);
					view.setVisibility(View.VISIBLE);					
				}
				else
				{
					View view = (View) event.getLocalState();
					view.setVisibility(View.VISIBLE);
					Context context = getApplicationContext();
					Toast.makeText(context, "you cant drop it here", Toast.LENGTH_LONG).show();
					break;
				}
				break;
				
			//the drag & drop operation has concluded
			case DragEvent.ACTION_DRAG_ENDED:
				//v.setBackground(normalShape); //go back to normal shape
				
				default:
					break;
			}
			return true;
		}
		
	}
}
